<?php
/**
 * 操作mysql的工具类，方便以后用，只做简单封装，以提高灵活性
 */
class dbClass{
    public $dbPort='3306';
    //public $charset='utf8';
    public $_linkID = null;
    public $queryID = null;
    public $numRows = null;
    public $numCols = null;

    function __construct($host,$user,$pass,$dbname){
        $this->_linkID = mysqli_connect($host, $user, $pass, $dbname, $this->dbPort);
        if (mysqli_connect_errno()) exit(mysqli_connect_error());

        $this->_linkID->query('set names utf8');
    }
    /*
     * @todo 查询指定的SQL并返回第一条记录
     */
    public function first($sql, $result_type = MYSQL_ASSOC) {
        return mysql_fetch_array($this->query($sql), $result_type);
    }
    /*
     * @todo 取所有结果集并以数组形式返回
     */
    public function fetchAll($sql,$result_type = MYSQL_ASSOC){
        $list = array();
        $result = $this->query($sql);
        while($row = @mysql_fetch_array($result, $result_type)){
            $list[] = $row;
        }
        return $list;
    }
    /*
     * @todo 更改操作
     */
    public function updatetable($tablename,$updatedata,$where = array()){
        $sets = array();
        $set = "";
        foreach ($updatedata as $k => $v) {
            $sets[] = '`' . $k . '` =  \'' . mysql_real_escape_string($v) . '\'';
        }
        $set = implode(',', $sets);
        if (!empty($where)){
            $wheres = array();
            foreach ($where as $k => $v) {
                $wheres[] = '`' . $k . '` = \'' . mysql_real_escape_string($v) . '\'';
            }
            $where = implode(' AND ', $wheres);
        }else {
            $where = ' 1 ';
        }
        $sql = "UPDATE `{$tablename}` SET $set WHERE $where";
        return $this->execute($sql);
    }
    /*
     * @todo SQL查询
     */
    public function query($sql) {
        $this->queryID = $this->_linkID->query($sql);

        // 对存储过程改进
        if( $this->_linkID->more_results() ){
            while (($res = $this->_linkID->next_result()) != NULL) {
                $res->free_result();
            }
        }

        if ( false === $this->queryID ) {
            $this->error();
            return false;
        } else {
            $this->numRows  = $this->queryID->num_rows;
            $this->numCols    = $this->queryID->field_count;
            return $this->getAll();
        }
    }

    /*
     * @todo SQL查询
     */
    public function execute($sql) {
        $this->queryID = $this->_linkID->query($sql);

        if ( false === $this->queryID ) {
            $this->error();
            return false;
        } else {
            $this->numRows  = $this->queryID->num_rows;
            $this->numCols    = $this->queryID->field_count;
            return $this->numRows;
        }
    }

    /**
     * 获得所有的查询数据
     * @access private
     * @param string $sql  sql语句
     * @return array
     */
    private function getAll() {
        //返回数据集
        $result = array();
        if($this->numRows>0) {
            //返回数据集
            for($i=0;$i<$this->numRows ;$i++ ){
                $result[$i] = $this->queryID->fetch_assoc();
            }
            $this->queryID->data_seek(0);
        }
        return $result;
    }
    /*
     * @todo 返回最后一次SQL操作的错误代码
     */
    function error() {
        $this->error = $this->_linkID->errno.':'.$this->_linkID->error;
        return $this->error;
    }
    /*
     * @todo 返回最后一次SQL操作的错误代码
     */
    function errno() {
        return intval(($this->_linkID) ? mysql_errno($this->link) : mysql_errno());
    }
}